define(function (require,exports,module) {
    var myPub = require('./public');
    var oBindMobileBg = $('.js_bindMobileBg');                          // 绑定手机号码弹窗
    var oCloseBindBtn = $('.js_closeBindBtn');                          // 关闭绑定弹窗按钮
    var oBindMobileInput = $('.js_bindMobileInput');                    // 绑定弹窗输入框
    var oGetMobile = $('.js_getMobile');                                // 填写手机号码输入框
    var oInputNum = $('.js_inputNum');                                  // 填写验证码输入框
    var oClearBindBtn = $('.js_clearBindBtn');                          // 清除内容输入框
    var oGetCodeNewBtn = $('.js_getCodeNewBtn');                        // 获取验证码按钮
    var oGetCodeCutDown = $('.js_getCodeCutDown');                      // 倒计时按钮
    var oCount = $('.js_count');                                        // 倒计时时间
    var oErrTip = $('.js_bindMobileErrTip');                            // 错误提示
    var oBindMobileBtn = $('.js_bindMobileBtn');                        // 确认按钮

    var bMobileNum = false;                                             // 手机号码是否合规
    var bCodeNum = false;                                               // 验证码是否合规
    var getCodeTap = true;                                              // 获取验证码按钮防止双击
    var getCodeTap_timer = null;                                        // 获取验证码按钮防止双击定时器
    var bindBtnTap = true;                                              // 确认按钮防双击
    var bindBtnTap_timer = null;                                        // 确认按钮防双击定时器
    var bScriptTencent = false;                                         // 腾讯的滑动验证码js是否加载过

    var verificationUrl = "/m/c/1.0/captcha/getTcCaptcha";              // 获取腾讯滑动验证码接口
    var senMsgCodeUrl = "/m/u/1.0/member/smsVerifyCode";                // 获取验证码的接口
    var bindLoginUrl = "/m/u/1.0/member/quickLogin2";                   // 快捷登录


    // 关闭绑定手机号码弹窗
    oCloseBindBtn.on('click',function (e) {
        oBindMobileBg.hide();
        e.stopPropagation();
    });

    // 对应的输入框显示对应的清除按钮
    oBindMobileInput.on('input propertychange',function (object) {
        var oClearBtn = $(this).siblings('.js_clearBindBtn');
        if($(this).val() != ''){
            oClearBtn.show();
        }
        if($(this).val() == ''){
            oClearBtn.hide();
        }
    });

    // 对应的清除按钮清除对应的输入框内容
    oClearBindBtn.on('click',function (e) {
        var oInput = $(this).siblings('.js_bindMobileInput');
        if(oInput.hasClass('js_getMobile')){
            bMobileNum = false
        }else if(oInput.hasClass('js_inputNum')){
            bCodeNum = false
        }
        oInput.val('');
        $(this).hide();
        oErrTip.text('');
        oBindMobileBtn.removeClass('btn_Bgred');
        oBindMobileBtn.addClass('btn_Bggay');
        e.stopPropagation();
    });

    // 输入手机号码
    oGetMobile.on('input propertychange', function () {
        myPub.checkMobile(oGetMobile,function () {
            // console.log('空')
            bMobileNum = false
        },function () {
            // console.log('不匹配')
            bMobileNum = false
        },function () {
            // console.log('正确');
            if(oGetMobile.val().length >= 11){
                bMobileNum = true
            }else{
                bMobileNum = false
            }
        });
        checkSureBtn()
    });

    // 输入验证码
    oInputNum.on('input propertychange', function () {
        checkCode(oInputNum,function () {
            // console.log('请输入验证码')
            bCodeNum = false
        },function () {
            // console.log('请输入正确的验证码')
            bCodeNum = false
        },function () {
            // console.log('验证码合规');
            if(oInputNum.val().length >= 6){
                bCodeNum = true
            }else{
                bCodeNum = false
            }
        });
        checkSureBtn()
    });

    // 获取验证码按钮
    oGetCodeNewBtn.on('click',function (e) {
       if(!getCodeTap)return;
        getCodeTap = false;
        if(!bMobileNum){
            if(oGetMobile.val() == ''){
                oErrTip.text('请输入手机号码')
            }else{
                oErrTip.text('请输入正确的手机号码')
            }
            getCodeTap = true
        }else{
            oErrTip.text('');

            if(bScriptTencent){
                getSlideTencentBind();
                return;
            }

            G_JSON_GET(verificationUrl,{
                mobile: oGetMobile.val()
            },function (data) {
                var dataItem = data.data;
                seriesLoadScripts(dataItem,function () {
                    bScriptTencent = true;
                    getSlideTencentBind();
                })
            });
            clearTimeout(getCodeTap_timer);
            getCodeTap_timer = setTimeout(function () {
                getCodeTap = true
            },300);
            getCodeTap = true
        }
        e.stopPropagation();
    });

    // 确认按钮
    oBindMobileBtn.on('click',function (e) {
       if(!bindBtnTap)return;
        bindBtnTap = false;
        // console.log($(this).hasClass('btn_Bgred'));
        if($(this).hasClass('btn_Bgred')){
            // console.log('确认');
            oErrTip.text('');
            G_JSON_POST(bindLoginUrl,{
                mobile: oGetMobile.val(),
                verifyCode: oInputNum.val()
            },function (data) {
                var dataArr = data;
                if(dataArr.ret.code == '0'){
                    myPub.setCookie("token", dataArr.data.tokenInfo.token);
                    myPub.TipLayerEnd('登录成功','no',function () {
                        oBindMobileBg.hide();
                        window.location.reload();
                    });
                }else{
                    myPub.TipLayer(dataArr.ret.message);
                }
            });
            clearTimeout(bindBtnTap_timer);
            bindBtnTap_timer = setTimeout(function () {
                bindBtnTap = true;
            },300)
        }else if(!bMobileNum){
            if(oGetMobile.val() == ''){
                oErrTip.text('请输入手机号码')
            }else{
                oErrTip.text('请输入正确的手机号码')
            }
            bindBtnTap = true;
        }else if(!bCodeNum){
            if(oInputNum.val() == ''){
                oErrTip.text('请输入验证码')
            }else{
                oErrTip.text('请输入正确的验证码')
            }
            bindBtnTap = true;
        }
        e.stopPropagation();
    });

    // 调用滑动验证模块 并获取验证码
    function getSlideTencentBind(){
        // 页面初始化
        var capOption = {callback: cbfn, showHeader: false, type: "poupup"};
        capInit(document.getElementById("TCaptcha"), capOption);
        //回调函数：验证码页面关闭时回调
        function cbfn(retJson) {
            if (retJson.ret == 0) {
                // 用户验证成功
                var tickets = retJson.ticket;
                // console.log(retJson.ticket);
                sendCord(tickets);
            }
            else {
                //用户关闭验证码页面，没有验证
                // console.log('验证错误');
                getCodeTap = true;
            }

        }
    }

    // 获取数字验证码接口
    function sendCord(tickets) {
        G_JSON_POST(senMsgCodeUrl,{
            mobile: oGetMobile.val(),
            ticket: tickets,
            smsType:'QUICK_LOGIN'
        },function (data) {
            var dataItem = data;
            if(dataItem.ret.code == '0'){
                setCodeCut(60);
                myPub.TipLayer(dataItem.data);
            }else{
                myPub.TipLayer(dataItem.ret.message);
            }
            // getCodeTap = true;
        })
    }

    // 发送验证码倒计时方法
    function setCodeCut (time) {
        var _time = time;
        oGetCodeNewBtn.hide();
        oGetCodeCutDown.show();
        myPub.CutTime(_time, function(_time){
            oGetCodeNewBtn.show();
            oGetCodeCutDown.hide();
            oCount.text(_time)
            },function(time){
            oCount.text(time)
        });
    }

    // 手机号码与验证码都合规确认按钮变红色
    function checkSureBtn(){
        if(bMobileNum &&　bCodeNum){
            oBindMobileBtn.addClass('btn_Bgred');
            oBindMobileBtn.removeClass('btn_Bggay');
        }else{
            oBindMobileBtn.removeClass('btn_Bgred');
            oBindMobileBtn.addClass('btn_Bggay');
        }
    }

    // 验证验证码是否合规
    function checkCode(obj,fn1,fn2,fn3) {
        var codeNum = obj.val();
        var reg = /^[0-9]*$/;       // 验证验证码
        if(codeNum == ''){
            if(fn1)fn1()
        }else if(!reg.test(codeNum)){
            if(fn2)fn2()
        }else{
            if(fn3)fn3()
        }
    }

    // 加载外部js
    function seriesLoadScripts(scripts, callback) {
        if (typeof (scripts) != "object") var scripts = [scripts];
        var HEAD = document.getElementsByTagName("head").item(0) || document.documentElement;
        var s = new Array(),
            last = scripts.length - 1,
            recursiveLoad = function (i) { //递归
                s[i] = document.createElement("script");
                s[i].setAttribute("type", "text/javascript");
                s[i].onload = s[i].onreadystatechange = function () { //Attach handlers for all browsers
                    if (!0 || this.readyState == "loaded" || this.readyState == "complete") {
                        this.onload = this.onreadystatechange = null;
                        this.parentNode.removeChild(this);
                        if (i != last) recursiveLoad(i + 1); else if (typeof (callback) == "function") callback();
                    }
                }
                s[i].setAttribute("src", scripts[i]);
                HEAD.appendChild(s[i]);
            };
        recursiveLoad(0);
    }


});